﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ImportSalesViaXML
{
    class ImportXML
    {
        static void Main(string[] args)
        {
            string file = "sales.xml";
            ImportSales(file);
        }
        public static void ImportSales(string fileName)
        {
            BookstoreDataClassesDataContext context = new BookstoreDataClassesDataContext();
            using (context)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(fileName);
                foreach (XmlNode node in doc.ChildNodes)
                {
                    foreach (XmlNode storeNode in node.ChildNodes)
                    {
                        int currStoreID = GetStoreId(storeNode, context);
                        string[] dateParts = (node.Attributes[0].InnerText).ToString().Split('/');
                        foreach (XmlNode sale in storeNode.ChildNodes)
                        {
                            int currBookID = GetBookId(sale, context);
                            if (currBookID == 0)
                            {
                                Console.WriteLine(sale.ChildNodes[0].InnerText + "is an invalid ISBN!");
                            }
                            else
                            {
                                Sale currentSale = new Sale()
                                {
                                    BookId = currBookID,
                                    BookStoreId = currStoreID,
                                    Amount = int.Parse(sale.ChildNodes[1].InnerText),
                                    SaleDate = new DateTime(int.Parse(dateParts[2]), int.Parse(dateParts[1]), int.Parse(dateParts[0]))

                                };
                                context.Sales.InsertOnSubmit(currentSale);
                                context.SubmitChanges();
                            }
                        }
                    }
                }
            }
        }

        public static int GetStoreId(XmlNode storeNode, BookstoreDataClassesDataContext context)
        {
            var stores =
                from s in context.BookStores
                select s;
            bool storeExist = false;
            int storeId = 0;
            foreach (var store in stores)
            {
                if (store.Name == storeNode.Attributes[0].InnerText)
                {
                    storeExist = true;
                    storeId = store.BookStoreId;
                    break;
                }
            }
            if (storeExist == false)
            {
                BookStore bookstore = new BookStore()
                {
                    Name = storeNode.Attributes[0].InnerText
                };

                context.BookStores.InsertOnSubmit(bookstore);
                context.SubmitChanges();
                return bookstore.BookStoreId;
            }
            else
            {
                return storeId;
            }
        }
        public static int GetBookId(XmlNode saleNode, BookstoreDataClassesDataContext context)
        {
            StringBuilder correctISBN = new StringBuilder();
            char[] currentISBN = saleNode.ChildNodes[0].InnerText.ToCharArray();
            for (int i = 0; i < currentISBN.Length; i++)
            {
                if (char.IsDigit(currentISBN[i]))
                {
                    correctISBN.Append(currentISBN[i]);
                }
            }
            var book =
    (from b in context.Books
     where b.ISBN == correctISBN.ToString()
     select b).FirstOrDefault();
            int bookId = 0;

            if (book != null)
            {
                bookId = book.BookId;
            }
            return bookId;

        }

    }
}
